
---
title: "Replication for ``How Information about Foreign Aid Affect Public Spending Decisions: Evidence from a Field Experiment in Malawi''"
author: "Ryan Jablonski"
date: "16 July 2020"
output:
  pdf_document:
    toc: yes
  word_document:
    toc: yes
  html_document:
    theme: united
    toc: yes
header-includes: \usepackage{float, makecell, longtable} 
editor_options:
  chunk_output_type: console
---

\section{Replication Notes}

This file creates a replication file containing the tables and figures from Seim, Brigitte, Ryan Jablonski and Johan Ahlback. "How Information about Foreign Aid Affect Public Spending Decisions: Evidence from a Field Experiment in Malawi" in the \textit{Journal of Development Economics}. 

This code was originally run on RStudio Version 1.2.5033 with R 3.5.0. All data has been anonymized to protect respondents. 

For questions about this replication contact Ryan Jablonski at r.s.jablonski@lse.ac.uk.

```{r setup, include=FALSE, echo=FALSE}
rm(list=ls(all=TRUE))
set.seed(81654)
rootdir="C:\\Users\\Ryan.LSE063315\\Dropbox\\Foreign Aid in Malawi\\Round 2 Experiment\\Analysis\\JDE Replication\\"

knitr::opts_knit$set(root.dir = rootdir)
setwd(rootdir)

library(ggplot2)
library(units)
library(survival)
library(lfe) 
library(lme4)
library(foreign)
library(broom)
library(dotwhisker)
library(dplyr)
library(stargazer)
library(MASS)
library(wesanderson)
library(corrplot)
library(dplyr)
library(xtable)



mp.survey=read.csv("./data/mp_withcovariates.csv", stringsAsFactors = FALSE)
c.survey=read.csv("./data/c_withcovariates.csv", stringsAsFactors = FALSE)
all.surveys=read.csv("./data/all_withcovariates.csv", stringsAsFactors = FALSE)
labels=read.csv("./data/labels.csv",stringsAsFactors=FALSE)

#these are labels for our variables to keep things pretty. We also use this to define the scope of control variables.
labels=subset(labels, !is.na(varname))
labels.all=labels
labels=labels[labels$include==1,]
labels = labels[order(labels$label),]
labels=labels[labels$alt.specific==1,]


#Create a vector of control variables to use in the analysis
controls = ""
for(i in c(1:nrow(labels))){
  controls = paste(controls, labels$varname[i], sep=" + ")
}

controls=paste(controls, "winner_percent_imp", sep=" + ")
controls=paste(controls, "any_children_attend_school", sep=" + ")
controls=gsub("school_need_index_constituency \\+ ", "", controls)
controls=gsub(" \\+  school_need_index_ward","", controls)



labelme=function(varname){
  this.label=""
  if(!grepl(":",varname)){
    this.label=labels.all[labels.all$varname==varname,]$label
  } else {
    varnames=strsplit(varname, ":")[[1]]
    this.label=labels.all[labels.all$varname==varnames[1],]$label
    for(i in 2:length(varnames)){
      this.label=paste(this.label, labels.all[labels.all$varname==varnames[i],]$label, sep="*")
    }
  }
  return(this.label)
}
  

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}



get_marginal_effect=function(this.xvar, this.pvar="donor_knowledge", this.model, vmatrix=NULL, p1=0){

  t1=1
  t0=0  
  beta <- coef(this.model)
  
  beta[!is.finite(beta)] <- 0
  
  covvar <- vcov(this.model)
  if(!is.null(vmatrix)){
    covvar=vmatrix
  }
  
  xvars=c(log(2), log(3), log(4), log(5))
  
  beta.sim <- mvrnorm(10000, beta,covvar)
  varnames=colnames(beta.sim)
  
  
  x0= matrix(0, ncol = length(varnames), nrow = length(xvars))
  x1= matrix(0, ncol = length(varnames), nrow = length(xvars))
  i=0
  for(this.name in varnames){
    i=i+1
    if(grepl(paste("^", this.xvar, "$", sep=""), this.name)){
      x0[,i]=xvars
      x1[,i]=xvars
      next
    }
    
    if(grepl(paste("^information_aid:", this.xvar, "$", sep=""), this.name)){
      x0[,i]=xvars*t0
      x1[,i]=xvars*t1
      next
    }
    
    if(this.name==paste("",this.xvar, ":", this.pvar, "", sep="")){
        
      x0[,i]=xvars*p1
      x1[,i]=xvars*p1
      next
    }
    
    if(this.name==paste("information_aid:",this.xvar,":", this.pvar, "", sep="")){
        
      x0[,i]=xvars*p1*t0
      x1[,i]=xvars*p1*t1
      next
    }
   
  }
  
  xb1 <- x1 %*% t(beta.sim)
  xb0 <- x0 %*% t(beta.sim)
  p1=xb1
  p0=xb0
  p1=exp(xb1)/(1+exp(xb1))
  p0=exp(xb0)/(1+exp(xb0))
  
  e=p1-p0
 
  predicted.e <- apply(e,1,quantile, probs =  c(.025,.5,.975))
  predictions=rbind(predicted.e, apply(e,1,mean), apply(e,1,sd))

  return(predictions)
}

```




\section{Table 1}
```{r tab_summary_stats_basic, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}

summarytable=data.frame(Variable=character(), All=character(), Treatment=character(), Control=character(), LCs=character(), MPs=character(), stringsAsFactors = FALSE)



varlist=data.frame(
  var_name=c("past_aid_project", "number_aid_categories", "school_need_index", "winner_percent_imp", "any_children_attend_school", "log_number_of_students", "pop_per_hectacre", "donor_knowledge"), stringsAsFactors = FALSE)
             



j=0
for(var in varlist$var_name){
  j=j+1
  this.df=all.surveys

  eval(parse(text=paste("this.df$this.var=this.df$", var, sep="")))
  
  N=nrow(this.df)
  N.treat=nrow(this.df[this.df$information_aid==1,])
  N.control=nrow(this.df[this.df$information_aid==0,])
  
  m.all=mean(this.df[!is.na(this.df$this.var),]$this.var)
  m.treat = mean(this.df[!is.na(this.df$this.var) & this.df$information_aid==1,]$this.var)
  m.control = mean(this.df[!is.na(this.df$this.var) & this.df$information_aid==0,]$this.var)
  m.lc = mean(this.df[!is.na(this.df$this.var) & this.df$mp==0,]$this.var)
  m.mp = mean(this.df[!is.na(this.df$this.var) & this.df$mp==1,]$this.var)
  
  s.all=sd(this.df[!is.na(this.df$this.var),]$this.var)
  s.treat = sd(this.df[!is.na(this.df$this.var) & this.df$information_aid==1,]$this.var)
  s.control = sd(this.df[!is.na(this.df$this.var) & this.df$information_aid==0,]$this.var)
  s.lc = sd(this.df[!is.na(this.df$this.var) & this.df$mp==0,]$this.var)
  s.mp = sd(this.df[!is.na(this.df$this.var) & this.df$mp==1,]$this.var)
  
  count=nrow(summarytable)+1
  summarytable[count,]$Variable=labelme(var)
  summarytable[count+1,]$Variable=""
  
  summarytable[count,]$All=round(m.all,3)
  summarytable[count+1,]$All=  paste("(", round(s.all,3), ")", sep="")                          

  summarytable[count,]$Treatment=round(m.treat,3)
  summarytable[count+1,]$Treatment=paste("(", round(s.treat,3), ")", sep="")
  summarytable[count,]$Control=round(m.control,3)
  summarytable[count+1,]$Control=  paste("(", round(s.control,3), ")", sep="")                          

  summarytable[count,]$MPs=round(m.mp,3)
  summarytable[count+1,]$MPs=paste("(", round(s.mp,3), ")", sep="")
  summarytable[count,]$LCs=round(m.lc,3)
  summarytable[count+1,]$LCs=  paste("(", round(s.lc,3), ")", sep="")                          

}

print(xtable(summarytable,  label="tab_summary_basic", caption="Sample Statistics"), include.rownames=FALSE,  caption.placement = "top", file=paste("output/tab_summary_basic.tex", sep=""), hline.after=c(-1, 0), tabular.environment = "longtable")
print(xtable(summarytable,  label="tab_summary_basic", caption="Sample Statistics"), include.rownames=FALSE,  caption.placement = "top", hline.after=c(-1, 0), tabular.environment = "longtable")
   

```



\section{Table 2}
```{r baseline tab_information_aid, message=FALSE, warning=FALSE, results='asis', echo=FALSE}


hd1.c = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", c.survey)  
hd1.mp = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey)
hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)

hd1.all.controls = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project", controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)  

stargazer(hd1.all, hd1.all.controls, hd1.c,  hd1.mp, type = "latex", 
          out=paste("./Output/tab_information_aid.tex", sep=""), 
          column.labels = c("All","All with Controls", "LCs",  "MPs"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="The Effect of Information about Past Aid",
          order=c("^information_aid:past_aid_project$", "^past_aid_project$"),
          keep=c("^information_aid:past_aid_project$", "^past_aid_project$"),
          covariate.labels = c(paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, sep="*\n"), labels.all[labels.all$varname=="past_aid_project",]$label),
          omit="*",
          no.space=TRUE,
          label=paste("tab_information_aid", sep=""),
          table.placement = "H"
          
)


```



\section{Table 3}
```{r tab_information_categories with number of aid categories, message=FALSE, warning=FALSE, results='asis', echo=FALSE}


#With categories
hd1.c = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", c.survey)  
hd1.mp = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey)
hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
hd1.all.controls = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories", controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)  


stargazer(hd1.all, hd1.all.controls, hd1.c,  hd1.mp, type = "latex", 
          out=paste("./Output/tab_information_categories.tex", sep=""), 
          column.labels = c("All","All with Controls", "LCs",  "MPs"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="The Effect of Information about Past Aid and Aid Categories",
          order=c("^information_aid:number_aid_categories$", "^number_aid_categories$"),
          keep=c("^information_aid:number_aid_categories$", "^number_aid_categories$"),
          covariate.labels = c(paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="number_aid_categories",]$label, sep="*\n"), labels.all[labels.all$varname=="number_aid_categories",]$label),
          omit="*",
          no.space=TRUE,
          label=paste("tab_information_categories.tex", sep=""),
          table.placement = "H"
          
)
```



\section{Table 4}
```{r all info treatments, message=FALSE, warning=FALSE, results='asis', echo=FALSE}

#need information treatment


h.all_info1 = clogit(as.formula(paste("incumbentselected ~  information_aid:past_aid_project +past_aid_project+                                      information_need:school_need_index +school_need_index+
                                    information_votes:winner_percent_imp +winner_percent_imp+
                                         strata(map_id) + 
                                      cluster(resp_id)")), method="efron", all.surveys)



h.all_info2 = clogit(as.formula(paste("incumbentselected ~  information_aid:number_aid_categories+number_aid_categories+                                      information_need:school_need_index +school_need_index+
                                    information_votes:winner_percent_imp +winner_percent_imp+
                                         strata(map_id) + 
                                      cluster(resp_id)")), method="efron", all.surveys)


h.need_info = clogit(as.formula(paste("incumbentselected ~information_need:school_need_index +school_need_index+strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)


h.voting_info = clogit(as.formula(paste("incumbentselected ~  information_votes:winner_percent_imp +winner_percent_imp+
                                         strata(map_id) + 
                                      cluster(resp_id)")), method="efron", all.surveys)


stargazer(h.all_info1, h.all_info2, h.need_info, h.voting_info, type = "latex", 
          out=paste("./Output/tab_all_info_treatments.tex", sep=""), 
          column.labels = c("\\makecell{All \\\\Treatments}","\\makecell{All \\\\Treatments}", "\\makecell{Need \\\\Treatments}", "\\makecell{Voting \\\\Treatments}"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "Standard errors are clustered on incumbent.",
          dep.var.caption  = "",
          title="Comparison with Other Information Treatments",
          order=c("^past_aid_project:information_aid$", "^past_aid_project$", 
                  "^number_aid_categories:information_aid$", "^number_aid_categories$",
                  "^school_need_index:information_need$",  "^school_need_index$", "winner_percent_imp:information_votes", "winner_percent_imp"),
          keep=c("^past_aid_project:information_aid$", "^past_aid_project$", "^number_aid_categories:information_aid$", "^number_aid_categories$","^school_need_index:information_need$",  "^school_need_index$", "winner_percent_imp:information_votes", "winner_percent_imp"),
          covariate.labels = c(labelme("past_aid_project:information_aid"), labelme("past_aid_project"),
                               labelme("number_aid_categories:information_aid"), labelme("number_aid_categories"),
                               labelme("school_need_index:information_need"),  labelme("school_need_index"), labelme("winner_percent_imp:information_votes"), labelme("winner_percent_imp")),
          omit="*",
          no.space=TRUE,
          label=paste("tab_all_info_treatments", sep=""),
          table.placement = "H"
)




```


\section{Figure 4}

```{r unconditional plot, message=FALSE, warning=FALSE, echo=FALSE}



ha.results=data.frame(term=NA, estimate=NA, std.error=NA, robust.se=NA, statistic=NA, p.value=NA, conf.low=NA, conf.high=NA, model=NA)

#HA.1. Politicians will allocate more aid to schools with high need than with low need.
ha1.c = clogit(as.formula(paste("incumbentselected ~ z_school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_need==0,])  
this.results=tidy(ha1.c)[1,]
this.results$model="LCs Only"
this.results$term="School Need Index"
ha.results=rbind(ha.results,this.results)

ha1.mp = clogit(as.formula(paste("incumbentselected ~ z_school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey[mp.survey$information_need==0,])  
this.results=tidy(ha1.mp)[1,]
this.results$model="MPs Only"
this.results$term="School Need Index"
ha.results=rbind(ha.results,this.results)

ha1.all = clogit(as.formula(paste("incumbentselected ~ z_school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys[all.surveys$information_need==0,])  
this.results=tidy(ha1.all)[1,]
this.results$model="All"
this.results$term="School Need Index"
ha.results=rbind(ha.results,this.results)

controls.t=gsub("school_need_index \\+", "", controls)

ha1.c.controls = clogit(as.formula(paste("incumbentselected ~ z_school_need_index", controls.t, "+ strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_need==0,])  
this.results=tidy(ha1.c.controls)[1,]
this.results$model="LCs with controls"
this.results$term="School Need Index"
ha.results=rbind(ha.results,this.results)

ha1.mp.controls = clogit(as.formula(paste("incumbentselected ~ z_school_need_index", controls.t, "+ strata(map_id) + cluster(resp_id)")), method="efron", mp.survey[mp.survey$information_need==0,])  
this.results=tidy(ha1.mp.controls)[1,]
this.results$model="MPs with controls"
this.results$term="School Need Index"
ha.results=rbind(ha.results,this.results)

ha1.all.controls = clogit(as.formula(paste("incumbentselected ~ z_school_need_index", controls.t, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys[all.surveys$information_need==0,])  
this.results=tidy(ha1.all.controls)[1,]
this.results$model="All with controls"
this.results$term="School Need Index"
ha.results=rbind(ha.results,this.results)




#HA.2. Politicians will allocate more aid to schools located in areas with higher support for the politician in the last election.
ha2.c = clogit(as.formula(paste("incumbentselected ~ z_winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_votes==0,])  
this.results=tidy(ha2.c)[1,]
this.results$model="LCs Only"
this.results$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results=rbind(ha.results,this.results)

ha2.mp = clogit(as.formula(paste("incumbentselected ~ z_winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey[mp.survey$information_votes==0,])  
this.results=tidy(ha2.mp)[1,]
this.results$model="MPs Only"
this.results$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results=rbind(ha.results,this.results)

ha2.all = clogit(as.formula(paste("incumbentselected ~ z_winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys[all.surveys$information_votes==0,])  
this.results=tidy(ha2.all)[1,]
this.results$model="All"
this.results$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results=rbind(ha.results,this.results)

controls.t=sub("winner_percent_imp \\+", "", controls)
ha2.c.controls = clogit(as.formula(paste("incumbentselected ~ z_winner_percent", controls.t," + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_votes==0,])  
this.results=tidy(ha2.c.controls)[1,]
this.results$model="LCs with controls"
this.results$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results=rbind(ha.results,this.results)

ha2.mp.controls = clogit(as.formula(paste("incumbentselected ~ z_winner_percent", controls.t," + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey[mp.survey$information_votes==0,])  
this.results=tidy(ha2.mp.controls)[1,]
this.results$model="MPs with controls"
this.results$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results=rbind(ha.results,this.results)

ha2.all.controls = clogit(as.formula(paste("incumbentselected ~ z_winner_percent", controls.t," + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys[all.surveys$information_votes==0,])  
this.results=tidy(ha2.all.controls)[1,]
this.results$model="All with controls"
this.results$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results=rbind(ha.results,this.results)


controls.t=controls
#HA.4. Politicians will allocate more aid to schools where their family members attend (Q1.54).
ha4.c.a = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school + strata(map_id) + cluster(resp_id)")),  method="efron",c.survey)  
this.results=tidy(ha4.c.a)[1,]
this.results$model="LCs Only"
this.results$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results=rbind(ha.results,this.results)

ha4.mp.a = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey)  
this.results=tidy(ha4.mp.a)[1,]
this.results$model="MPs Only"
this.results$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results=rbind(ha.results,this.results)

ha4.all.a = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)  
this.results=tidy(ha4.all.a)[1,]
this.results$model="All"
this.results$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results=rbind(ha.results,this.results)

controls.t=sub("any_children_attend_school \\+", "", controls)
controls.t=sub("\\+ any_children_attend_school", "", controls)
ha4.c.a.controls = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron",c.survey)  
this.results=tidy(ha4.c.a.controls)[1,]
this.results$model="LCs with controls"
this.results$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results=rbind(ha.results,this.results)

ha4.mp.a.controls = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school", controls.t, " + strata(map_id) + cluster(resp_id)")), method="efron", mp.survey)  
this.results=tidy(ha4.mp.a.controls)[1,]
this.results$model="MPs with controls"
this.results$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results=rbind(ha.results,this.results)

ha4.all.a.controls = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school", controls.t, " + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)  
this.results=tidy(ha4.all.a.controls)[1,]
this.results$model="All with controls"
this.results$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results=rbind(ha.results,this.results)


#HA.6 politicians will allocate more/less to places with more aid projects.
ha6.c = clogit(as.formula(paste("incumbentselected ~ z_past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_aid==0,])  
this.results=tidy(ha6.c)[1,]
this.results$model="LCs Only"
this.results$term=labels.all[labels.all$varname=="past_aid_project",]$label
ha.results=rbind(ha.results,this.results)

ha6.mp = clogit(as.formula(paste("incumbentselected ~ z_past_aid_project + strata(map_id) + cluster(resp_id)")),  method="efron", mp.survey[mp.survey$information_aid==0,])  
this.results=tidy(ha6.mp)[1,]
this.results$model="MPs Only"
this.results$term=labels.all[labels.all$varname=="past_aid_project",]$label
ha.results=rbind(ha.results,this.results)

ha6.all = clogit(as.formula(paste("incumbentselected ~ z_past_aid_project + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys[all.surveys$information_aid==0,])  
this.results=tidy(ha6.all)[1,]
this.results$model="All"
this.results$term=labels.all[labels.all$varname=="past_aid_project",]$label
ha.results=rbind(ha.results,this.results)

controls.t=sub("\\+ past_aid_project", "", controls)

ha6.c.controls = clogit(as.formula(paste("incumbentselected ~ z_past_aid_project", controls.t, " + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_aid==0,])  
this.results=tidy(ha6.c.controls)[1,]
this.results$model="LCs with controls"
this.results$term=labels.all[labels.all$varname=="past_aid_project",]$label
ha.results=rbind(ha.results,this.results)

ha6.mp.controls = clogit(as.formula(paste("incumbentselected ~ z_past_aid_project", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", mp.survey[mp.survey$information_aid==0,])  
this.results=tidy(ha6.mp.controls)[1,]
this.results$model="MPs with controls"
this.results$term=labels.all[labels.all$varname=="past_aid_project",]$label
ha.results=rbind(ha.results,this.results)

ha6.all.controls = clogit(as.formula(paste("incumbentselected ~ z_past_aid_project", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys[all.surveys$information_aid==0,])  
this.results=tidy(ha6.all.controls)[1,]
this.results$model="All with controls"
this.results$term=labels.all[labels.all$varname=="past_aid_project",]$label
ha.results=rbind(ha.results,this.results)


#HA.6 politicians will allocate more/less to places with more aid project categories. 
ha6.c = clogit(as.formula(paste("incumbentselected ~ z_number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_aid==0,])  
this.results=tidy(ha6.c)[1,]
this.results$model="LCs Only"
this.results$term=labels.all[labels.all$varname=="number_aid_categories",]$label
ha.results=rbind(ha.results,this.results)

ha6.mp = clogit(as.formula(paste("incumbentselected ~ z_number_aid_categories + strata(map_id) + cluster(resp_id)")),  method="efron", mp.survey[mp.survey$information_aid==0,])  
this.results=tidy(ha6.mp)[1,]
this.results$model="MPs Only"
this.results$term=labels.all[labels.all$varname=="number_aid_categories",]$label
ha.results=rbind(ha.results,this.results)

ha6.all = clogit(as.formula(paste("incumbentselected ~ z_number_aid_categories + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys[all.surveys$information_aid==0,])  
this.results=tidy(ha6.all)[1,]
this.results$model="All"
this.results$term=labels.all[labels.all$varname=="number_aid_categories",]$label
ha.results=rbind(ha.results,this.results)


controls.t=sub("\\+ past_aid_project", "", controls)

ha6.c.controls = clogit(as.formula(paste("incumbentselected ~ z_number_aid_categories", controls.t, " + strata(map_id) + cluster(resp_id)")), method="efron", c.survey[c.survey$information_aid==0,])  
this.results=tidy(ha6.c.controls)[1,]
this.results$model="LCs with controls"
this.results$term=labels.all[labels.all$varname=="number_aid_categories",]$label
ha.results=rbind(ha.results,this.results)

ha6.mp.controls = clogit(as.formula(paste("incumbentselected ~ z_number_aid_categories", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", mp.survey[mp.survey$information_aid==0,])  
this.results=tidy(ha6.mp.controls)[1,]
this.results$model="MPs with controls"
this.results$term=labels.all[labels.all$varname=="number_aid_categories",]$label
ha.results=rbind(ha.results,this.results)

ha6.all.controls = clogit(as.formula(paste("incumbentselected ~ z_number_aid_categories", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys[all.surveys$information_aid==0,])  
this.results=tidy(ha6.all.controls)[1,]
this.results$model="All with controls"
this.results$term=labels.all[labels.all$varname=="number_aid_categories",]$label
ha.results=rbind(ha.results,this.results)


ha7.c = clogit(as.formula(paste("incumbentselected ~ z_log_number_of_students + strata(map_id) + cluster(resp_id)")),  method="efron", c.survey[c.survey$information_need==0,])  
this.results=tidy(ha7.c)[1,]
this.results$model="LCs Only"
this.results$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results=rbind(ha.results,this.results)

ha7.mp = clogit(as.formula(paste("incumbentselected ~ z_log_number_of_students + strata(map_id) + cluster(resp_id)")),  method="efron",  mp.survey[mp.survey$information_need==0,])  
this.results=tidy(ha7.mp)[1,]
this.results$model="MPs Only"
this.results$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results=rbind(ha.results,this.results)

ha7.all = clogit(as.formula(paste("incumbentselected ~ z_log_number_of_students + strata(map_id) + cluster(resp_id)")),  method="efron",  all.surveys[all.surveys$information_need==0,])  
this.results=tidy(ha7.all)[1,]
this.results$model="All"
this.results$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results=rbind(ha.results,this.results)



controls.t=sub("log_number_of_students \\+", "", controls)
ha7.c.controls = clogit(as.formula(paste("incumbentselected ~ z_log_number_of_students", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron",  c.survey[c.survey$information_need==0,])  
this.results=tidy(ha7.c.controls)[1,]
this.results$model="LCs with controls"
this.results$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results=rbind(ha.results,this.results)

ha7.mp.controls = clogit(as.formula(paste("incumbentselected ~ z_log_number_of_students", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron",  mp.survey[mp.survey$information_need==0,])  
this.results=tidy(ha7.mp.controls)[1,]
this.results$model="MPs with controls"
this.results$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results=rbind(ha.results,this.results)

ha7.all.controls = clogit(as.formula(paste("incumbentselected ~ z_log_number_of_students", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron",  all.surveys[all.surveys$information_need==0,])  
this.results=tidy(ha7.all.controls)[1,]
this.results$model="All with controls"
this.results$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results=rbind(ha.results,this.results)

#HA.7 population density
controls.t=controls
ha7.c.controls = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", c.survey)  
this.results=tidy(ha7.c.controls)[1,]
this.results$model="LCs with controls"
this.results$term=labels.all[labels.all$varname=="pop_per_hectacre",]$label
ha.results=rbind(ha.results,this.results)

ha7.mp.controls = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", mp.survey)  
this.results=tidy(ha7.mp.controls)[1,]
this.results$model="MPs with controls"
this.results$term=labels.all[labels.all$varname=="pop_per_hectacre",]$label
ha.results=rbind(ha.results,this.results)

ha7.all.controls = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre", controls.t, " + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys)  
this.results=tidy(ha7.all.controls)[1,]
this.results$model="All with controls"
this.results$term=labels.all[labels.all$varname=="pop_per_hectacre",]$label
ha.results=rbind(ha.results,this.results)


ha7.c = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre + strata(map_id) + cluster(resp_id)")),  method="efron", c.survey)  
this.results=tidy(ha7.c)[1,]
this.results$model="LCs Only"
this.results$term=labels.all[labels.all$varname=="pop_per_hectacre",]$label
ha.results=rbind(ha.results,this.results)

ha7.mp = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre + strata(map_id) + cluster(resp_id)")),  method="efron", mp.survey)  
this.results=tidy(ha7.mp)[1,]
this.results$model="MPs Only"
this.results$term=labels.all[labels.all$varname=="pop_per_hectacre",]$label
ha.results=rbind(ha.results,this.results)

ha7.all = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys)  
this.results=tidy(ha7.all)[1,]
this.results$model="All"
this.results$term=labels.all[labels.all$varname=="pop_per_hectacre",]$label
ha.results=rbind(ha.results,this.results)


ha.results$exp_estimate = exp(ha.results$estimate)
ha.results=ha.results[!is.na(ha.results$model),]

ha.results=ha.results[order(ha.results$model, ha.results$model),]


ha.results=ha.results[ha.results$model %in% c("All", "All with controls", "MPs Only", "LCs Only"),]

unplot=dwplot(ha.results[!is.na(ha.results$estimate),], 
       vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.4),
       dot_args = list(aes(shape = model))
)+     theme_bw() + xlab("Marginal Effect (log odds)") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(0.55, 0.01),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank()) +
  scale_color_manual(values=wes_palette(n=4, name="Darjeeling1"))+
    scale_shape_discrete(name = "model")+
    coord_cartesian(xlim=c(-0.5, 1.1))


pdf("./Output/unconditional_plot.pdf",width=5, height=5, family="Times")
  unplot
dev.off()
unplot
```



\section{Figure 5}

```{r Conditional Effects, message=FALSE, warning=FALSE, echo=FALSE}


ha.results=data.frame(term=NA, estimate=NA, std.error=NA, robust.se=NA, statistic=NA, p.value=NA, conf.low=NA, conf.high=NA, model=NA, predictor=NA)

hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*factor(donor_knowledge>0.4) + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_knowledge > 0.4)TRUE", this.model=hd1.all, p1=0 )
this.results=tidy(hd1.all)[1,]
this.results$model="Qs Correct = 0"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1]
this.results$robust.se=est[5,1]
this.results$p.value=2*pt(est[4,1]/est[5,1], df=(hd1.all$n-6))
this.results$predictor="A: Foreign Aid Project Knowledge"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_knowledge > 0.4)TRUE", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Qs Correct > 0"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$robust.se=est[5,1]
this.results$p.value=2*pt(est[4,1]/est[5,1], df=(hd1.all$n-6))
this.results$predictor="A: Foreign Aid Project Knowledge"
ha.results=rbind(ha.results,this.results)


all.surveys$donor_frequency=ifelse(all.surveys$donor_frequency>4, 4, all.surveys$donor_frequency)


hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*factor(donor_frequency) + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_frequency)1", this.model=hd1.all, p1=0 )
this.results=tidy(hd1.all)[1,]
this.results$model="Never"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="B: Donor Interaction Frequency"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_frequency)1", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Rarely"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="B: Donor Interaction Frequency"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_frequency)2", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="A few times per year"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="B: Donor Interaction Frequency"
ha.results=rbind(ha.results,this.results)


est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_frequency)3", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Once a month"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="B: Donor Interaction Frequency"
ha.results=rbind(ha.results,this.results)


est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(donor_frequency)4", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Weekly or more"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="B: Donor Interaction Frequency"
ha.results=rbind(ha.results,this.results)

all.surveys$length_of_residence_index=ifelse(all.surveys$length_of_residence_index==0, 1, all.surveys$length_of_residence_index)

hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*factor(length_of_residence_index) + strata(map_id) + cluster(resp_id)")), method="efron", data=all.surveys)
est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(length_of_residence_index)1", this.model=hd1.all, p1=0 )
this.results=tidy(hd1.all)[1,]
this.results$model="Less than 5 years"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="C: Length of Residence"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(length_of_residence_index)1", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="5 to 10 years"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="C: Length of Residence"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(length_of_residence_index)2", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="More than 10 years"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="C: Length of Residence"
ha.results=rbind(ha.results,this.results)


est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(length_of_residence_index)3", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="All my life"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="C: Length of Residence"
ha.results=rbind(ha.results,this.results)

hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*factor(useful) + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(useful)1", this.model=hd1.all, p1=0 )
this.results=tidy(hd1.all)[1,]
this.results$model="Not very useful"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="D: Usefulness of Information"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(useful)1", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Somewhat useful"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="D: Usefulness of Information"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="factor(useful)2", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Very useful"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="D: Usefulness of Information"
ha.results=rbind(ha.results,this.results)


hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*learn + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="learn", this.model=hd1.all, p1=0 )
this.results=tidy(hd1.all)[1,]
this.results$model="No"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$robust.se=est[5,1]
this.results$p.value=2*pt(est[4,1]/est[5,1], df=(hd1.all$n-6))
this.results$predictor="E: Learning"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="learn", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Yes"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="E: Learning"
this.results$robust.se=est[5,1]
this.results$p.value=2*pt(est[4,1]/est[5,1], df=(hd1.all$n-6))
ha.results=rbind(ha.results,this.results)


#test of tearfund interaction
#summary(clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*tearfundinteract + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys))
#summary(clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*tearfundheard + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys))


hd1.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*tearfundheard + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="tearfundheard", this.model=hd1.all, p1=0 )
this.results=tidy(hd1.all)[1,]
this.results$model="No"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$robust.se=est[5,1]
this.results$p.value=2*pt(est[4,1]/est[5,1], df=(hd1.all$n-6))
this.results$predictor="F: Heard of Tearfund"
ha.results=rbind(ha.results,this.results)

est=get_marginal_effect(this.xvar="past_aid_project", this.pvar="tearfundheard", this.model=hd1.all, p1=1 )
this.results=tidy(hd1.all)[1,]
this.results$model="Yes"
this.results$term=""
this.results$estimate=est[4,1]
this.results$conf.low =est[1,1] 
this.results$conf.high =est[3,1] 
this.results$predictor="F: Heard of Tearfund"
this.results$robust.se=est[5,1]
this.results$p.value=2*pt(est[4,1]/est[5,1], df=(hd1.all$n-6))
ha.results=rbind(ha.results,this.results)


ha.results=ha.results[!is.na(ha.results$model),]

levels(ha.results$predictor) <- c("A: Foreign Aid Project Knowledge", "B: Donor Interaction Frequency", "C: Length of Residence", "D: Usefulness of Information", "E: Learning", "F: Heard of Tearfund") 

setwd(rootdir)
pdf("./Output/priorinteractions.pdf",width=7,height=6, family="Times")

p1=dwplot(ha.results[ha.results$predictor=="A: Foreign Aid Project Knowledge",], 
          vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.5),
          dot_args = list(aes(shape = model))
)+  facet_grid(.~predictor) + coord_flip() +   
  theme_bw() + xlab("Treatment Effect") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(1.05, 0.0),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank(),
        legend.margin=margin(t = 0.1,l=0.1,r=0.1,b=0.1, unit='cm'),
        text = element_text(size=9),
        legend.key.size = unit(0.7, 'lines'),
        plot.margin=unit(c(0,86.5,-10,5),"points")) +
  scale_color_manual(values=wes_palette(n=5, name="Darjeeling1")) +
  scale_shape_discrete(name = "model")

p4=dwplot(ha.results[ha.results$predictor=="B: Donor Interaction Frequency",], 
          vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.5),
          dot_args = list(aes(shape = model))
)+  facet_grid(.~predictor) + coord_flip() +   
  theme_bw() + xlab("Treatment Effect") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(1.05, 0.0),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank(),
        legend.margin=margin(t = 0.1,l=0.1,r=0.1,b=0.1, unit='cm'),
        text = element_text(size=9),
        legend.key.size = unit(0.7, 'lines'),
        plot.margin=unit(c(0,86.5,-10,5),"points")) +
  scale_color_manual(values=wes_palette(n=5, name="Darjeeling1")) +
  scale_shape_discrete(name = "model")



p2=dwplot(ha.results[ha.results$predictor=="C: Length of Residence",], 
          vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.5),
          dot_args = list(aes(shape = model))
)+  facet_grid(.~predictor) + coord_flip() +   
  theme_bw() + xlab("Treatment Effect") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(1.05, 0),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank(),
        legend.margin=margin(t = 0.1,l=0.1,r=0.1,b=0.1, unit='cm'),
        text = element_text(size=9),
        legend.key.size = unit(0.7, 'lines'),
        plot.margin=unit(c(0,86.5,-10,5),"points")) +
  scale_color_manual(values=wes_palette(n=5, name="Darjeeling1")) +
  scale_shape_discrete(name = "model")



p5=dwplot(ha.results[ha.results$predictor=="D: Usefulness of Information",], 
          vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.5),
          dot_args = list(aes(shape = model))
)+  facet_grid(.~predictor) + coord_flip() +   
  theme_bw() + xlab("Treatment Effect") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(1.05, 0),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank(),
        legend.margin=margin(t = 0.1,l=0.1,r=0.1,b=0.1, unit='cm'),
        text = element_text(size=9),
        legend.key.size = unit(0.7, 'lines'),
        plot.margin=unit(c(0,86.5,-10,5),"points")) +
  scale_color_manual(values=wes_palette(n=5, name="Darjeeling1")) +
  scale_shape_discrete(name = "model")


p3=dwplot(ha.results[ha.results$predictor=="E: Learning",], 
          vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.5),
          dot_args = list(aes(shape = model))
)+  facet_grid(.~predictor) + coord_flip() +   
  theme_bw() + xlab("Treatment Effect") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(1.05, 0),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank(),
        legend.margin=margin(t = 0.1,l=0.1,r=0.1,b=0.1, unit='cm'),
        text = element_text(size=9),
        legend.key.size = unit(0.7, 'lines'),
        plot.margin=unit(c(0,86.5,-10,5),"points")) +
  scale_color_manual(values=wes_palette(n=5, name="Darjeeling1")) +
  scale_shape_discrete(name = "model")



p6=dwplot(ha.results[ha.results$predictor=="F: Heard of Tearfund",], 
          vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.5),
          dot_args = list(aes(shape = model))
)+  facet_grid(.~predictor) + coord_flip() +   
  theme_bw() + xlab("Treatment Effect") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(1.05, 0),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.2),
        legend.title = element_blank(),
        legend.margin=margin(t = 0.1,l=0.1,r=0.1,b=0.1, unit='cm'),
        text = element_text(size=9),
        legend.key.size = unit(0.7, 'lines'),
        plot.margin=unit(c(0,86.5,-10,5),"points")) +
  scale_color_manual(values=wes_palette(n=5, name="Darjeeling1")) +
  scale_shape_discrete(name = "model")


multiplot(p1, p2, p3,p4,p5, p6, cols=2)

dev.off()

multiplot(p1, p2, p3,p4,p5, p6, cols=2)


```



\section{Figure 6}

```{r Effect of School Characteristics, message=FALSE, warning=FALSE, echo=FALSE}


this.survey=all.surveys
controls=gsub(" \\+ school_need_index_constituency","", controls)
controls=gsub(" \\+ school_need_index_ward","", controls)

ha.results=data.frame(term=NA, estimate=NA, std.error=NA, robust.se=NA, statistic=NA, p.value=NA, conf.low=NA, conf.high=NA, model=NA, predictor=NA)

this.controls=gsub(" \\+ winner_percent_imp","", controls)
h1 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_winner_percent + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
t=1
m=h1
ha.results[1,]$estimate = m$coefficients["z_winner_percent"] + m$coefficients["information_aid:z_winner_percent"]*t
ha.results[1,]$robust.se = sqrt(coef(summary(m))["z_winner_percent", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_winner_percent", "robust se"]^2 + 2*t*vcov(m)["z_winner_percent","information_aid:z_winner_percent"])
ha.results[1,]$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results[1,]$p.value=coef(summary(m))["information_aid:z_winner_percent", "Pr(>|z|)"]

ha.results[1,]$model="Treatment"
t=0
ha.results[2,]$estimate = m$coefficients["z_winner_percent"] + m$coefficients["information_aid:z_winner_percent"]*t
ha.results[2,]$robust.se = sqrt(coef(summary(m))["z_winner_percent", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_winner_percent", "robust se"]^2 + 2*t*vcov(m)["z_winner_percent","information_aid:z_winner_percent"])
ha.results[2,]$term=labels.all[labels.all$varname=="winner_percent_imp",]$label
ha.results[2,]$model="Control"


this.controls=gsub(" \\+ school_need_index","", controls)
h2 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_school_need_index + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
m=h2
t=1
ha.results[3,]$estimate = m$coefficients["z_school_need_index"] + m$coefficients["information_aid:z_school_need_index"]*t
ha.results[3,]$robust.se = sqrt(coef(summary(m))["z_school_need_index", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_school_need_index", "robust se"]^2 + 2*t*vcov(m)["z_school_need_index","information_aid:z_school_need_index"])
ha.results[3,]$term=labels.all[labels.all$varname=="school_need_index",]$label
ha.results[3,]$p.value=coef(summary(m))["information_aid:z_school_need_index", "Pr(>|z|)"]

ha.results[3,]$model="Treatment"
t=0
ha.results[4,]$estimate = m$coefficients["z_school_need_index"] + m$coefficients["information_aid:z_school_need_index"]*t
ha.results[4,]$robust.se = sqrt(coef(summary(m))["z_school_need_index", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_school_need_index", "robust se"]^2 + 2*t*vcov(m)["z_school_need_index","information_aid:z_school_need_index"])
ha.results[4,]$term=labels.all[labels.all$varname=="school_need_index",]$label
ha.results[4,]$model="Control"

this.controls=gsub(" \\+ any_children_attend_school","", controls)
h3 = clogit(as.formula(paste("incumbentselected ~ information_aid*any_children_attend_school + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
m=h3
t=1
ha.results[5,]$estimate = m$coefficients["any_children_attend_school"] + m$coefficients["information_aid:any_children_attend_school"]*t
ha.results[5,]$robust.se = sqrt(coef(summary(m))["any_children_attend_school", "robust se"]^2 + t^2*coef(summary(m))["information_aid:any_children_attend_school", "robust se"]^2 + 2*t*vcov(m)["any_children_attend_school","information_aid:any_children_attend_school"])
ha.results[5,]$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results[5,]$p.value=coef(summary(m))["information_aid:any_children_attend_school", "Pr(>|z|)"]

ha.results[5,]$model="Treatment"

t=0
ha.results[6,]$estimate = m$coefficients["any_children_attend_school"] + m$coefficients["information_aid:any_children_attend_school"]*t
ha.results[6,]$robust.se = sqrt(coef(summary(m))["any_children_attend_school", "robust se"]^2 + t^2*coef(summary(m))["information_aid:any_children_attend_school", "robust se"]^2 + 2*t*vcov(m)["any_children_attend_school","information_aid:any_children_attend_school"])
ha.results[6,]$term=labels.all[labels.all$varname=="any_children_attend_school",]$label
ha.results[6,]$model="Control"

this.controls=gsub(" \\+ log_number_of_students","", controls)
h4 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_log_number_of_students + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
m=h4
t=1
ha.results[7,]$estimate = m$coefficients["z_log_number_of_students"] + m$coefficients["information_aid:z_log_number_of_students"]*t
ha.results[7,]$robust.se = sqrt(coef(summary(m))["z_log_number_of_students", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_log_number_of_students", "robust se"]^2 + 2*t*vcov(m)["z_log_number_of_students","information_aid:z_log_number_of_students"])
ha.results[7,]$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results[7,]$p.value=coef(summary(m))["information_aid:z_log_number_of_students", "Pr(>|z|)"]
ha.results[7,]$model="Treatment"
t=0
ha.results[8,]$estimate = m$coefficients["z_log_number_of_students"] + m$coefficients["information_aid:z_log_number_of_students"]*t
ha.results[8,]$robust.se = sqrt(coef(summary(m))["z_log_number_of_students", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_log_number_of_students", "robust se"]^2 + 2*t*vcov(m)["z_log_number_of_students","information_aid:z_log_number_of_students"])
ha.results[8,]$term=labels.all[labels.all$varname=="log_number_of_students",]$label
ha.results[8,]$model="Control"


h5 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_log_number_of_students + information_aid*any_children_attend_school + information_aid*z_winner_percent + information_aid*z_school_need_index + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  

this.controls=gsub(" \\+ temporary_to_permanent_ratio","", controls)
h6 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_temporary_to_permanent_ratio + information_aid*past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
m=h6
t=1
ha.results[9,]$estimate = m$coefficients["z_temporary_to_permanent_ratio"] + m$coefficients["information_aid:z_temporary_to_permanent_ratio"]*t
ha.results[9,]$robust.se = sqrt(coef(summary(m))["z_temporary_to_permanent_ratio", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_temporary_to_permanent_ratio", "robust se"]^2 + 2*t*vcov(m)["z_temporary_to_permanent_ratio","information_aid:z_temporary_to_permanent_ratio"])
ha.results[9,]$term=labels.all[labels.all$varname=="temporary_to_permanent_ratio",]$label
ha.results[9,]$p.value=coef(summary(m))["information_aid:z_temporary_to_permanent_ratio", "Pr(>|z|)"]

ha.results[9,]$model="Treatment"
t=0
ha.results[10,]$estimate = m$coefficients["z_temporary_to_permanent_ratio"] + m$coefficients["information_aid:z_temporary_to_permanent_ratio"]*t
ha.results[10,]$robust.se = sqrt(coef(summary(m))["z_temporary_to_permanent_ratio", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_temporary_to_permanent_ratio", "robust se"]^2 + 2*t*vcov(m)["z_temporary_to_permanent_ratio","information_aid:z_temporary_to_permanent_ratio"])
ha.results[10,]$term=labels.all[labels.all$varname=="temporary_to_permanent_ratio",]$label
ha.results[10,]$model="Control"

this.controls=gsub(" \\+ student_to_classroom_ratio","", controls)
h7 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_student_to_classroom_ratio + information_aid*past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
m=h7
t=1
ha.results[11,]$estimate = m$coefficients["z_student_to_classroom_ratio"] + m$coefficients["information_aid:z_student_to_classroom_ratio"]*t
ha.results[11,]$robust.se = sqrt(coef(summary(m))["z_student_to_classroom_ratio", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_student_to_classroom_ratio", "robust se"]^2 + 2*t*vcov(m)["z_student_to_classroom_ratio","information_aid:z_student_to_classroom_ratio"])
ha.results[11,]$term=labels.all[labels.all$varname=="student_to_classroom_ratio",]$label
ha.results[11,]$p.value=coef(summary(m))["information_aid:z_student_to_classroom_ratio", "Pr(>|z|)"]

ha.results[11,]$model="Treatment"
t=0
ha.results[12,]$estimate = m$coefficients["z_student_to_classroom_ratio"] + m$coefficients["information_aid:z_student_to_classroom_ratio"]*t
ha.results[12,]$robust.se = sqrt(coef(summary(m))["z_student_to_classroom_ratio", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_student_to_classroom_ratio", "robust se"]^2 + 2*t*vcov(m)["z_student_to_classroom_ratio","information_aid:z_student_to_classroom_ratio"])
ha.results[12,]$term=labels.all[labels.all$varname=="student_to_classroom_ratio",]$label
ha.results[12,]$model="Control"

this.controls=gsub(" \\+ student_to_teacher_ratio","", controls)
h8 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_student_to_teacher_ratio + information_aid*past_aid_project ", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
m=h8
t=1
ha.results[13,]$estimate = m$coefficients["z_student_to_teacher_ratio"] + m$coefficients["information_aid:z_student_to_teacher_ratio"]*t
ha.results[13,]$robust.se = sqrt(coef(summary(m))["z_student_to_teacher_ratio", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_student_to_teacher_ratio", "robust se"]^2 + 2*t*vcov(m)["z_student_to_teacher_ratio","information_aid:z_student_to_teacher_ratio"])
ha.results[13,]$term=labels.all[labels.all$varname=="student_to_teacher_ratio",]$label
ha.results[13,]$p.value=coef(summary(m))["information_aid:z_student_to_teacher_ratio", "Pr(>|z|)"]
ha.results[13,]$model="Treatment"
t=0
ha.results[14,]$estimate = m$coefficients["z_student_to_teacher_ratio"] + m$coefficients["information_aid:z_student_to_teacher_ratio"]*t
ha.results[14,]$robust.se = sqrt(coef(summary(m))["z_student_to_teacher_ratio", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_student_to_teacher_ratio", "robust se"]^2 + 2*t*vcov(m)["z_student_to_teacher_ratio","information_aid:z_student_to_teacher_ratio"])
ha.results[14,]$term=labels.all[labels.all$varname=="student_to_teacher_ratio",]$label
ha.results[14,]$model="Control"


ha.results$conf.low=ha.results$estimate-qt(0.975,df=h1$n-1)*ha.results$robust.se
ha.results$conf.high=ha.results$estimate +qt(0.975,df=h1$n-1)*ha.results$robust.se

ha.results$exp_estimate = exp(ha.results$estimate)
ha.results=ha.results[!is.na(ha.results$model),]

ha.results=ha.results[order(ha.results$model, ha.results$model),]
ha.results=ha.results[!is.na(ha.results$estimate),]
           
pdf("./Output/interaction_plot.pdf",width=4.5,height=4.5, family="Times")

dwplot(ha.results, 
       vline = geom_vline(xintercept = 0, colour = "black", linetype = 2, size=0.4),
       dot_args = list(aes(shape = model))
)+
  annotate("text", y = (1:nrow(ha.results[ha.results$model=="Treatment",])), x = rep(-0.45, nrow(ha.results[ha.results$model=="Treatment",])), label = paste("  p=", rev(round(signif(ha.results[ha.results$model=="Treatment",]$p.value,3),2))), size=3)+
  theme_bw() + xlab("Marginal Effect (Log Odds)") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(0.6, 0.01),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="black", size=0.3),
        legend.title = element_blank()) +
  scale_shape_discrete(name = "model")+
  coord_cartesian(xlim=c(-0.6, 1.5))+
  scale_color_manual(values=wes_palette(n=2, name="Darjeeling1"))

dev.off()
knitr::include_graphics("../Output/interaction_plot.pdf")

```


\section{Table 5}

```{r, message=FALSE, warning=FALSE, echo=FALSE,  results='asis'}



stargazer(h1, h2, h3, h4, type = "latex", 
          out=paste("./Output/tab_interactions.tex", sep=""), 
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="The Effect of Treatment by School Characteristic",
          order=c("^information_aid:z_winner_percent$", "^information_aid:z_school_need_index$", "^information_aid:any_children_attend_school$", "^information_aid:z_log_number_of_students$", "^information_aid:z_past_aid_project$", "^z_past_aid_project$", "^z_winner_percent$", "^z_school_need_index$", "^any_children_attend_school$", "^z_log_number_of_students$"),
          keep=c("^information_aid:z_winner_percent$", "^information_aid:z_school_need_index$", "^information_aid:any_children_attend_school$", "^information_aid:z_log_number_of_students$", "^information_aid:z_past_aid_project$", "^z_past_aid_project$", "^z_winner_percent$", "^z_school_need_index$", "^any_children_attend_school$", "^z_log_number_of_students$"),
          covariate.labels = c(
            paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="winner_percent_imp",]$label, sep="*\n"), 
            paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="school_need_index",]$label, sep="*\n"), 
            paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="any_children_attend_school",]$label, sep="*\n"), 
            paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="log_number_of_students",]$label, sep="*\n"), 
            paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, sep="*\n"), 
            labels.all[labels.all$varname=="past_aid_project",]$label,
            labels.all[labels.all$varname=="winner_percent_imp",]$label,
            labels.all[labels.all$varname=="school_need_index",]$label,          
            labels.all[labels.all$varname=="any_children_attend_school",]$label,  
            labels.all[labels.all$varname=="log_number_of_students",]$label), 
           
          omit="*",
          no.space=TRUE,
          label=paste("tab_interactions", sep=""),
          table.placement = "H",
          column.sep.width="1pt"
          
)


```